package com.zeta.system.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.zeta.api.client.system.IUserApi;
import com.zeta.api.model.UserInfoDTO;
import com.zeta.system.model.entity.SysUser;
import com.zeta.system.service.ISysUserService;
import io.swagger.v3.oas.annotations.Hidden;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.zetaframework.controller.SuperBaseController;

import java.util.Collections;
import java.util.List;

/**
 * 系统服务-用户 Feign接口实现
 *
 * @author gcc
 */
@Hidden // swagger文档忽略
@Slf4j
@RestController
@RequestMapping("/system")
@RequiredArgsConstructor
public class UserApiImpl implements SuperBaseController, IUserApi {

    private final ISysUserService userService;

    /**
     * 获取用户基础信息
     *
     * @param id 用户id
     * @return UserInfoDTO
     */
    @Override
    @GetMapping("/user/info/{id}")
    public UserInfoDTO getUserInfoById(@PathVariable("id") Long id) {
        SysUser user = userService.getById(id);
        if (user == null) { return null; }

        return BeanUtil.toBean(user, UserInfoDTO.class);
    }

    /**
     * 批量获取用户基础信息
     *
     * @param ids 用户id列表
     * @return List<UserInfoDTO>
     */
    @Override
    @GetMapping("/user/info/batch")
    public List<UserInfoDTO> getUserInfoByIds(@RequestParam("ids") List<Long> ids) {
        List<SysUser> users = userService.listByIds(ids);
        if (CollUtil.isEmpty(users)) { return Collections.emptyList(); }

        return BeanUtil.copyToList(users, UserInfoDTO.class);
    }
}
